<?php
/**
 * Created by PhpStorm.
 * User: godjarvis
 * Date: 2020/08/05
 * Time: 09:28
 */

namespace App\Http\Controllers;

use App\Http\Requests\Index\LoginRequest;
use App\Model\Service\UserService;
use Fresh\Annotation\Autowired;
use Fresh\Annotation\RequestMapping;
use Fresh\Foundation\Traits\Part\Session;
use Fresh\Mvc\Controller;

class indexController extends Controller
{
    use Session;

    /**
     * @Autowired()
     * @var UserService
     */
    public $userService;

    /**
     * 登录获取token
     * @RequestMapping(value="/auth/token/get")
     */
    public function authAction(LoginRequest $request)
    {
        return $this->userService->login($request->username, $request->password);
    }

    /**
     * 检测 token 有效性
     * @RequestMapping(value="/auth/token/check")
     */
    public function checkAction()
    {
        return [
            'access_token' => $this->session->getToken(),
            'is_valid'     => $this->session->getUserId() > 0
        ];
    }

    /**
     * 登出
     * @RequestMapping(value="/auth/token/logout")
     */
    public function logoutAction()
    {
        return $this->session->flush();
    }
}
